function ECSiteController(){ }
ECSiteController.prototype.params = {};
ECSiteController.prototype.tabs = [];
ECSiteController.prototype.curTabIndex = 0;

ECSiteController.prototype.freeMemory = function(isPageHome) {
    $("#listProductsView").html("");
    // $('#ec-navbar-list').html("");
    
    ECSiteController.prototype.tabs = null;
    if (isPageHome) {
        ECSiteController.prototype.params = null;
        ECSiteController.prototype.curTabIndex = 0;
    }
};
    
$(function(){
    
    $(document).on('pagebeforeshow', '#pageECsite', function(){      
        monaca.updateUIStyle("footer", "visibility", "false");
        Util.prototype.changeSnapperElement("pageECsite-content");
        
        var ecShops = ECSiteController.prototype.params.ecShops;
        var firstClickedEcCode = ECSiteController.prototype.params.firstClickedEcCode;
        
        var list_navbar = '<div data-role="navbar" id="ec-navbar"><ul>';
        ECSiteController.prototype.tabs = [];
        
        for(var i = 0; i < ecShops.length; i ++) {
            var code = ecShops[i].ecsite_code;
            var name = DetailProductService.prototype.getEcNameByECcode(code);
            var isCurrentTab = (code == firstClickedEcCode);
            list_navbar += '<li><a  ' + (isCurrentTab ? 'class="ui-btn-active"' : '') + ' class="nav-bar-title" tab="' + i + '"  id="navbar-' + code + '" href="">' + name + '</a></li>';
            ECSiteController.prototype.tabs.push({"code": code, "maxItems": ecShops[i].numberOfItems, "pageNo": 1, "data": []});
            if (isCurrentTab) {
                ECSiteController.prototype.curTabIndex = i;
                monaca.updateUIStyle("titlelable", "text", name);
            }
        }
        list_navbar += '</ul></div>';

        $('#ec-navbar').remove();
        $('#ec-navbar-wapper').append($(list_navbar));
        $('#ec-navbar').navbar();

        ECSiteController.prototype.loadDataFromServer();
    });
    
    $(document).on('pageshow', '#pageECsite', function(){
        PageChangeController.prototype.freeMemory();
    });
    
    $(document).on("tap","#ec-navbar a",function(){
        var currentTabId = parseInt($(this).attr("tab"), 10);
        if (currentTabId == ECSiteController.prototype.curTabIndex) {
            return false;
        }
        Util.prototype.showSpinner();
        ECSiteController.prototype.curTabIndex = currentTabId;
        var curTab = ECSiteController.prototype.tabs[ECSiteController.prototype.curTabIndex];
        monaca.updateUIStyle("titlelable", "text", DetailProductService.prototype.getEcNameByECcode(curTab.code));
        var listProductsView = $("#listProductsView");
        listProductsView.html('');
        var listHtml = '';
        if (curTab.data.length > 0) {            
            for (var i = 0; i < curTab.data.length; i ++) {
                listHtml += getRowHtml(curTab.data[i]);
            }            
        } else {
            if (curTab.pageNo != -1) { // get from Server
                ECSiteController.prototype.loadDataFromServer();
                return false;
            } else {
                listHtml = '<h3>このショップには当該する商品がありません</h3>';                
            }            
        }
        listProductsView.append(listHtml);
        listProductsView.listview("refresh");
        
        if (curTab.pageNo == -1) {
            $(".ecsite-pull").hide();
        } else {
            $(".ecsite-pull").show();
        }
        Util.prototype.hideSpinner();
    });
    
    $(document).on("tap",".shopDetailWebLink", function() {
        monaca.invokeBrowser($(this).attr("web_url"));
    });

    function getRowHtml(curItem) {
        return '<table class="productList shopDetailWebLink" web_url= "'+ curItem.itemUrl+
                '" ><tr><td colspan="2"><div style="overflow: hidden;">'+curItem.itemShortName+
                '</div></td></tr><tr><td rowspan="2" class="itemImage" style="background-image:url('+
                curItem.smallImageUrl+');"></td></tr><tr><td class="itemNameDate"><div style="overflow: hidden; margin-bottom:10px;><p class="detail">店名: <span class="shopName">'+
                curItem.shopName+'</span></p></div><p class="registered detail"><span class="price">'+
                curItem.formattedItemPrice+'円</span></p></td></tr></table>';
    }                          

    ECSiteController.prototype.loadDataFromServer = function(callback) {
        $(document).data('loading', true);
        var curTab = ECSiteController.prototype.tabs[ECSiteController.prototype.curTabIndex];
        
        var options = { keyword: ECSiteController.prototype.params.barcode_number,
                           ecsite_code: curTab.code,
                           page: curTab.pageNo,
                       };
        DetailProductService.prototype.getItemInfo({action: "findbykeyword", options: options}, function(data) {
            var listHtml = '';
            if (data.items.length > 0) {                
                for (var i = 0; i < data.items.length; i ++) {
                    var curItem = data.items[i];
                    ECSiteController.prototype.tabs[ECSiteController.prototype.curTabIndex].data.push(curItem);
                    listHtml += getRowHtml(curItem);
                }
                if ((data.items.length < Util.prototype.maxBookmarkNumber) || 
                        ((curTab.pageNo * Util.prototype.maxBookmarkNumber) >= curTab.maxItems)) {
                    ECSiteController.prototype.tabs[ECSiteController.prototype.curTabIndex].pageNo = -1;
                    $(".ecsite-pull").hide();
                } else {
                    ECSiteController.prototype.tabs[ECSiteController.prototype.curTabIndex].pageNo ++;
                }
            } else {
                if (curTab.pageNo == 1) {
                    listHtml = "<h3> 商品が見つかりません</h3>";
                }
                ECSiteController.prototype.tabs[ECSiteController.prototype.curTabIndex].pageNo = -1;
                $(".ecsite-pull").hide();
            }
            $("#listProductsView").append(listHtml).listview("refresh");

            if (typeof callback != 'undefined'){
                callback();
            }
            $(document).data('loading', false);
            Util.prototype.hideSpinner();
        }, function(err) {
            if (curTab.pageNo == 1) {
                $("#listProductsView").append("<h3> 商品が見つかりません</h3>").listview("refresh");
            }
            ECSiteController.prototype.tabs[ECSiteController.prototype.curTabIndex].pageNo = -1;
            $(".ecsite-pull").hide();
            
            Util.prototype.hideSpinner();
        });
    };    
});
